package com.openapi.protocol.encode;

import com.openapi.data.JT808PackageData;
import com.openapi.data.JT808PushData;
import exsun.jt808.data.distribute_data.DistributeEntity;
import exsun.jt808.data.distribute_data.jt808.*;
import exsun.jt808.data.upload_data.Jt808EntityBase;
import exsun.jt808.data.upload_data.jt808.LatLng;
import exsun.jt808.data.util.parse.BCD8421Operater;
import exsun.jt808.data.util.parse.BitOperator;
import exsun.jt808.data.util.parse.DecoderUtils;

public class Package8082019 extends Package808 {



    //查询服务器时间应答
    public static JT808PushData queryServerTimeResponse_0x8004(QueryServerTimeResponse data)
    {
        byte[] body = BitOperator.concatAll(BCD8421Operater.DateToBCD6(data.getServerTime()));
        return new JT808PushData(0x8004,body,data);
    }

    //查询区域或线路数据
    public static JT808PushData  queryAreaOrLineData_0x8608(QueryAreaOrLineData data) {
        byte[] body = BitOperator.concatAll(
                BitOperator.integerTo1Bytes(data.getQueryType()),
                BitOperator.integerTo4Bytes(data.getLineIds().size()));
        for (Integer item : data.getLineIds()) {
            body = BitOperator.concatAll(
                    body,
                    BitOperator.integerTo4Bytes(item));
        }
        return new JT808PushData(0x8608,body,data);
    }

    //链路检测
    public static JT808PushData linkDetection_0x8204(DistributeEntity data)
    {
        byte[] msgBodyBytes = new byte[0];
        return new JT808PushData(0x8204, msgBodyBytes, data);
    }

    //文本信息下发
    public  JT808PushData text_0x8300(TextSend data)
    {

        String[] hexFlag = new String[8];
        hexFlag[0] = String.valueOf(data.getSignType());
        hexFlag[1] = String.valueOf(data.getSignType());
        if (data.getSignType() == 1){
            String.valueOf(hexFlag[0] == "1" && hexFlag[1] == "0");
        }else if (data.getSignType() == 2){
            String.valueOf(hexFlag[0] == "0" && hexFlag[1] == "1");
        }else if (data.getSignType() == 3){
            String.valueOf(hexFlag[0] == "1" && hexFlag[1] == "1");
        }
        hexFlag[2] = data.isTerminalDisplay() ? "1" : "0";
        hexFlag[3] = data.isTtsPlay() ? "1" : "0";
        hexFlag[5] = String.valueOf(data.getMsgType());
        int flag = Integer.parseInt("00" + hexFlag[5] + "0" + hexFlag[3] + hexFlag[2] + hexFlag[1] + hexFlag[0], 2);
        byte[] msgBodyBytes = BitOperator.concatAll(
                BitOperator.integerTo1Bytes(flag),
                BitOperator.integerTo1Bytes(data.getTextType()),
                BitOperator.stringToBytes(data.getContent()));
        return new JT808PushData(0x8300, msgBodyBytes, data);
    }

    //车辆控制
    public  JT808PushData controlVehicle_0x8500(ControlVehicle data)
    {
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo2Bytes(data.getControlNum()),
                BitOperator.integerTo2Bytes(data.getControlId()));
        if (data.getControlId() == 0x0001){
            msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getCmd()));
        }
        return new JT808PushData(0x8500, msgBodyBytes, data);
    }

    //设置圆形区域
    public  JT808PushData setCircleArea_0x8600(SetCircleArea data)
    {
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getFlag()),
                BitOperator.integerTo1Bytes(data.getItems().size()));
        for (CircleAreaInfo item :
                data.getItems())
        {
            int areaFlag = getAreaProp_Circle(item.getAreaProperty());
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo4Bytes(item.getAreaId()),
                    BitOperator.integerTo2Bytes(areaFlag),
                    BitOperator.integerTo4Bytes((int) (item.getCenterLat() * 1000000)),
                    BitOperator.integerTo4Bytes((int) (item.getCenterLng() * 1000000)),
                    BitOperator.integerTo4Bytes(item.getRadius()));
            if (item.getAreaProperty().isbTime())
            {
                msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                        BCD8421Operater.DateToBCD6(item.getBeginTime()),
                        BCD8421Operater.DateToBCD6(item.getEndTime()));
            }
            if (item.getAreaProperty().isbSpeed())
            {
                msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                        BitOperator.integerTo2Bytes(item.getMaxSpeed()),
                        BitOperator.integerTo1Bytes(item.getOverSpeedTime()),
                        BitOperator.integerTo2Bytes(item.getMaxSpeedInNight()));
            }
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo2Bytes(item.getAreaName().length()),
                    BitOperator.stringToBytes(item.getAreaName()));
        }
        return new JT808PushData(0x8600, msgBodyBytes, data);
    }

    private static int getAreaProp_Circle(AreaProperty item)
    {
        String[] hexFlag = new String[16];
        hexFlag[0] = item.isbTime() ? "1" : "0";
        hexFlag[1] = item.isbSpeed() ? "1" : "0";
        hexFlag[2] = item.isbInToDriver() ? "1" : "0";
        hexFlag[3] = item.isbInToPlat() ? "1" : "0";
        hexFlag[4] = item.isbOutToDriver() ? "1" : "0";
        hexFlag[5] = item.isbOutToPlat() ? "1" : "0";
        hexFlag[6] = String.valueOf(item.getLatType());
        hexFlag[7] = String.valueOf(item.getLongType());
        hexFlag[8] = String.valueOf(item.getAllowOpenDoor());
        hexFlag[9] = "0";
        hexFlag[10] = "0";
        hexFlag[11] = "0";
        hexFlag[12] = "0";
        hexFlag[13] = "0";
        hexFlag[14] = String.valueOf(item.getOpenCommunicationModule());
        hexFlag[15] = String.valueOf(item.getCollectGNSSInfo());
        String str = "";
        for (int n = hexFlag.length - 1; n <= 0; n--)
        {
            str += hexFlag[n];
        }
        int areaFlag = Integer.parseInt(str, 2);
        return areaFlag;
    }

    //设置矩形区域
    public  JT808PushData setRectanglArea_0x8602(SetRectanglArea data)
    {
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getFlag()),
                BitOperator.integerTo1Bytes(data.getItems().size()));
        for (RectanglAreaInfo item :
                data.getItems())
        {
            int areaFlag = getAreaProp_SetRectangl(item.getAreaProperty());
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo4Bytes(item.getAreaId()),
                    BitOperator.integerTo2Bytes(areaFlag),
                    BitOperator.integerTo4Bytes((int) (item.getLeftTopLat() * 1000000)),
                    BitOperator.integerTo4Bytes((int) (item.getLeftTopLng() * 1000000)),
                    BitOperator.integerTo4Bytes((int) (item.getRightBelowLat() * 1000000)),
                    BitOperator.integerTo4Bytes((int) (item.getRightBelowLng() * 1000000)));
            if (item.getAreaProperty().isbTime())
            {
                msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                        BCD8421Operater.DateToBCD6(item.getBeginTime()),
                        BCD8421Operater.DateToBCD6(item.getEndTime()));
            }
            if (item.getAreaProperty().isbSpeed())
            {
                msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                        BitOperator.integerTo2Bytes(item.getMaxSpeed()),
                        BitOperator.integerTo1Bytes(item.getOverSpeedTime()),
                        BitOperator.integerTo2Bytes(item.getMaxSpeedInNight()));
            }
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo2Bytes(item.getAreaName().length()),
                    BitOperator.stringToBytes(item.getAreaName()));
        }
        return new JT808PushData(0x8602, msgBodyBytes, data);
    }

    private static int getAreaProp_SetRectangl(AreaProperty item)
    {
        String[] hexFlag = new String[16];
        hexFlag[0] = item.isbTime() ? "1" : "0";
        hexFlag[1] = item.isbSpeed() ? "1" : "0";
        hexFlag[2] = item.isbInToDriver() ? "1" : "0";
        hexFlag[3] = item.isbInToPlat() ? "1" : "0";
        hexFlag[4] = item.isbOutToDriver() ? "1" : "0";
        hexFlag[5] = item.isbOutToPlat() ? "1" : "0";
        hexFlag[6] = String.valueOf(item.getLatType());
        hexFlag[7] = String.valueOf(item.getLongType());
        hexFlag[8] = String.valueOf(item.getAllowOpenDoor());
        hexFlag[9] = "0";
        hexFlag[10] = "0";
        hexFlag[11] = "0";
        hexFlag[12] = "0";
        hexFlag[13] = "0";
        hexFlag[14] = String.valueOf(item.getOpenCommunicationModule());
        hexFlag[15] = String.valueOf(item.getCollectGNSSInfo());
        String str = "";
        for (int n = hexFlag.length - 1; n <= 0; n--)
        {
            str += hexFlag[n];
        }
        int areaFlag = Integer.parseInt(str, 2);
        return areaFlag;
    }

    //设置多边形区域
    public  JT808PushData setPolygonArea_0x8604(SetPolygonArea data)
    {
        int areaFlag = getAreaProp_SetPolygon(data.getAreaProperty());
        byte[] msgBodyBytes = BitOperator.concatAll(
                BitOperator.integerTo4Bytes(data.getAreaId()),
                BitOperator.integerTo2Bytes(areaFlag));
        if (data.getAreaProperty().isbTime())
        {
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BCD8421Operater.DateToBCD6(data.getBeginTime()),
                    BCD8421Operater.DateToBCD6(data.getEndTime()));
        }
        if (data.getAreaProperty().isbSpeed())
        {
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo2Bytes(data.getMaxSpeed()),
                    BitOperator.integerTo1Bytes(data.getOverSpeedTime()));
        }
        msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                BitOperator.integerTo2Bytes(data.getLatLngs().size()));
        for (LatLng item : data.getLatLngs())
        {
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo4Bytes((int) (item.getLat() * 1000000)),
                    BitOperator.integerTo4Bytes((int) (item.getLng() * 1000000)));
        }
        if (data.getAreaProperty().isbSpeed()){
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo2Bytes(data.getMaxSpeedInNight()));
        }
        msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                BitOperator.integerTo2Bytes(data.getAreaName().length()),
                BitOperator.stringToBytes(data.getAreaName()));

        return new JT808PushData(0x8604, msgBodyBytes, data);
    }

    private static int getAreaProp_SetPolygon(AreaProperty item)
    {
        String[] hexFlag = new String[16];
        hexFlag[0] = item.isbTime() ? "1" : "0";
        hexFlag[1] = item.isbSpeed() ? "1" : "0";
        hexFlag[2] = item.isbInToDriver() ? "1" : "0";
        hexFlag[3] = item.isbInToPlat() ? "1" : "0";
        hexFlag[4] = item.isbOutToDriver() ? "1" : "0";
        hexFlag[5] = item.isbOutToPlat() ? "1" : "0";
        hexFlag[6] = String.valueOf(item.getLatType());
        hexFlag[7] = String.valueOf(item.getLongType());
        hexFlag[8] = String.valueOf(item.getAllowOpenDoor());
        hexFlag[9] = "0";
        hexFlag[10] = "0";
        hexFlag[11] = "0";
        hexFlag[12] = "0";
        hexFlag[13] = "0";
        hexFlag[14] = String.valueOf(item.getOpenCommunicationModule());
        hexFlag[15] = String.valueOf(item.getCollectGNSSInfo());
        String str = "";
        for (int n = hexFlag.length - 1; n <= 0; n--)
        {
            str += hexFlag[n];
        }
        int areaFlag = Integer.parseInt(str, 2);
        return areaFlag;
    }

    //设置路线
    public  JT808PushData setRoute_0x8606(SetRoute data)
    {
        int routeProp = getRouteProp(data.getRouteProperty());
        byte[] msgBodyBytes = BitOperator.concatAll(
                BitOperator.integerTo4Bytes(data.getRouteId()),
                BitOperator.integerTo2Bytes(routeProp));
        if (data.getRouteProperty().isbTime())
        {
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BCD8421Operater.DateToBCD6(data.getBeginTime()),
                    BCD8421Operater.DateToBCD6(data.getEndTime()));
        }
        msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                BitOperator.integerTo2Bytes(data.getInflexions().size()));
        for (InflexionInfo item :
                data.getInflexions())
        {
            int lingProp = getLineProp(item.getLineProperty());
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo4Bytes(item.getInflexionId()),
                    BitOperator.integerTo4Bytes(item.getLineId()),
                    BitOperator.integerTo4Bytes((int) (item.getLat() * 1000000)),
                    BitOperator.integerTo4Bytes((int) (item.getLng() * 1000000)),
                    BitOperator.integerTo1Bytes(item.getLineWith()),
                    BitOperator.integerTo1Bytes(lingProp));
            if (item.getLineProperty().isbTime())
            {
                msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                        BitOperator.integerTo2Bytes(item.getLineDriverOutTime()),
                        BitOperator.integerTo2Bytes(item.getLineDriverShortTime()));
            }
            if (item.getLineProperty().isbSpeed())
            {
                msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                        BitOperator.integerTo2Bytes(item.getLineMaxSpeed()),
                        BitOperator.integerTo1Bytes(item.getLineOverSpeedTime()),
                        BitOperator.integerTo2Bytes(item.getMaxSpeedInNight()));
            }
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo2Bytes(data.getAreaName().length()),
                    BitOperator.stringToBytes(data.getAreaName()));
        }
        return new JT808PushData(0x8606, msgBodyBytes, data);
    }

    private static int getRouteProp(RouteProperty item)
    {
        String[] hexFlag = new String[8];
        hexFlag[0] = item.isbTime() ? "1" : "0";
        hexFlag[1] = "0";
        hexFlag[2] = item.isbInToDriver() ? "1" : "0";
        hexFlag[3] = item.isbInToPlat() ? "1" : "0";
        hexFlag[4] = item.isbOutToDriver() ? "1" : "0";
        hexFlag[5] = item.isbOutToPlat() ? "1" : "0";
        hexFlag[6] = "0";
        hexFlag[7] = "0";
        String str = "";
        for (int n = hexFlag.length - 1; n <= 0; n--)
        {
            str += hexFlag[n];
        }
        int areaFlag = Integer.parseInt(str, 2);
        return areaFlag;
    }

    private static int getLineProp(LineProperty item)
    {
        String[] hexFlag = new String[8];
        hexFlag[0] = item.isbTime() ? "1" : "0";
        hexFlag[1] = item.isbSpeed() ? "1" : "0";
        hexFlag[2] = String.valueOf(item.getLatType());
        hexFlag[3] = String.valueOf(item.getLongType());
        hexFlag[4] = "0";
        hexFlag[5] = "0";
        hexFlag[6] = "0";
        hexFlag[7] = "0";
        String str = "";
        for (int n = hexFlag.length - 1; n <= 0; n--)
        {
            str += hexFlag[n];
        }
        int areaFlag = Integer.parseInt(str, 2);
        return areaFlag;
    }

    //数据下行透传
    public JT808PushData issueOSPFDecoder(IssueOSPF data){
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getMsgType()),BitOperator.stringToBytes(data.getContent()));
        return new JT808PushData(0x8900, msgBodyBytes, data);
    }


}
